﻿using UnityEngine;
using System.Collections;
using ProtoBuf.Message;
using System.Collections.Generic;

namespace Login
{
    public class LoginGenerateName:Fsm.FsmStateMonoBase<LoginState>
    {
        LoginManager manager = null;
        public LoginGenerateName(LoginState state, LoginManager ctrl) : base(state, ctrl) { manager = ctrl; }


        GameObject storyObj;
        public override void Enter()
        {
            isStoryPlayed = false;
        }

        void StoryStart()
        {
            storyObj = ManagerController.Instance.ShowEnterGameStory();
            storyObj.GetComponent<Story2dEnterGame>().endcallback = OnStoryEnd;
        }

        void OnStoryEnd()
        {
            GameObject.Destroy(storyObj);
            AssetLoad.ResourceManager.Instance.UnloadUnusedResources();
            manager.StartCoroutine(LoadOver());
        }


        public IEnumerator LoadOver()
        {
            LoadingPanel.Instance.ShowMask(0);
            yield return AssetLoad.AssetBundleManager.Instance.StartCoroutine(AssetLoad.AssetBundleManager.Instance.LoadSceneAsync("map/" + SceneName.selectHero, SceneName.selectHero));
            yield return new WaitForEndOfFrame();
            manager.nickName.GetComponent<ChoseRole>().manager = manager;
            manager.nickName.SetActive(true);

            manager.loginUI.HideNormalUI();
            manager.loginUI.HideServerPanel();
            manager.loginUI.loginBG.SetActive(false);
            CameraTool.Instance.sceneCamera.enabled = false;
            LoadingPanel.Instance.HideLoading();
        }
        //void GenerateName() 
        //{
        //    CSGenerateName msg = new CSGenerateName();
        //    msg.gender = 0;//暂时写0
        //    NetAPI.SendHttp(OpDefine.CSGenerateName, msg, GenerateNameCallBack);
        //    Debug.Log("generateName");
        //}

        //void GenerateNameCallBack(NetWork.Packets.Packet packet, bool bSuccess) 
        //{
        //    if (bSuccess)
        //    {
        //        SCGenerateName msg = packet.kBody as SCGenerateName;
        //        if (msg != null)
        //        {
        //            Application.LoadLevel("map_select_001");
        //            manager.nickName.GetComponent<InputNickName>().nickNameInput.GetComponent<UIInput>().label.GetComponent<UILabel>().text = msg.playerName;
        //            Debug.Log("随机生成的名字是：" + msg.playerName);
        //        }
        //    }
        //} 
        public override void Leave()
        {
            base.Leave();
        }
        bool isStoryPlayed = false;

        public override void Update()
        {
            if (!LoginUI.firstCoroutine && !isStoryPlayed)
            {
                StoryStart();
                isStoryPlayed = true;
            }
        }
    }
}
